'use strict';

const express = require('express');
const router = express.Router();
const { filePath } = require('../configPath');
const menu_file_path = filePath('klein-web-data/_menu_db.json');
const { Read2WriteFile, ReadFile, Success, Error } = require('@/utils/utils');

/**
 * 菜单新增
 */
router.post('/menu/submit', async (req, res) => {
  let result = null;
  try {
    // console.log(req.query); get
    console.log(req.body); // post

    // list/page 列表
    // remove 删除
    // submit 新增
    // detail 详情
    // enable 禁用

    const msg = await Read2WriteFile(menu_file_path, req.body);
    result = Success(req, {
      msg,
    });
  } catch (err) {
    result = Error(err);
  }

  return res.send(result);
});

/**
 * 菜单列表
 */
router.get('/menu/list', async (req, res) => {
  let result = null;
  try {
    const data = await ReadFile(menu_file_path);
    result = Success(req, data);
  } catch (err) {
    result = Error(err);
  }
  return res.send(result);
});

module.exports = router;
